<template>
     <div class="search">
        <van-search                  
            v-model="key"
            show-action
            clearable
            placeholder="输入关键字"
            @search="onSearch"
            @update:model-value	='handeValueChanged'>
            <template #action>
                <div @click="onClickSearchButton" class="searchBtn">
                    搜索
                </div>
            </template>
            <template #left-icon>
                <img :src="icon_search"  style="width:.4rem;height: .4rem;margin: 0 .3rem;"/>
            </template>
        </van-search>
    </div>
</template>
<script setup lang="ts">
import { ref,defineEmits, watch, onMounted } from 'vue';
import { useSearchStore } from '@/stores';

const searchStore = useSearchStore();

const props = defineProps({
    text:String
})

const emiter = defineEmits(['search']);


onMounted(()=>{
    console.log('register sub,in searchbox')

    searchStore.$onAction((args)=>{
        if(args.name == 'setKey'){
            if(args.args[0]){
                key.value = args.args[0]
            }else{
                key.value = ''
            }

        }
    })

    
})
 



const key = ref('');
const onSearch = ()=>{
    emiter("search",key.value)
}
const onClickSearchButton = ()=>{
    emiter("search",key.value)
}

const handeValueChanged = (value:string)=>{
    console.log('search value changed'+value)
    searchStore.setKey(value);
}

import icon_search from '@/assets/icons/icon_search.png'

</script>
<style lang="less">
      .search{
            background-color: #fff;
            padding: 10px 3%;
             
            
            display: flex;
            
            flex-flow: row nowrap;
            
            justify-content: flex-start;
            
            align-items: center;
            width: 100%;
            margin: 0 auto;
            z-index: 2;

            .van-search{
                flex-grow: 1;
                height: .8rem;
                overflow: hidden;


                .searchBtn{
                    color:#1D4ED8;
                    font-size: 16px;
                }

                .van-search__content{
                    
                    margin-right: 3%;
                    border-radius: 5px;
                    background-color: #f2f2f2;
                    overflow: hidden;
                    height: .8rem;
                    padding-left: 0;

                    .van-field__control{
                        font-size: 16px;
                        background-color: #f2f2f2;
                        border: none;
                        width: calc(100% - 2.5rem);
                        color: #333;
                        font-size: 16px;
                        line-height: .8rem;
                        height: .8rem;
                        flex-grow: 1;
                    }
                }
            }

            
        }
</style>

<style lang="css">
   
 @media screen and (min-width: 1024px) {
  
    .search{
        width:670px !important;
        margin:auto;
        
    }
}
</style>